
Int_t ProcessWaveFrametmp( vector<int> waveframe,PMTtest &pmtdata,float range_factor) {
float baseline=pmtdata.Calculatebsl(waveframe);
//cout<<"the baslineis:"<<baseline<<endl;
pmtdata.Setbaseline(baseline);

//find signal crosstime for full frame; for the study of later pulse of PMT
//fill the crosstime for full frame 
// LED trigger region
int inte_left=pmtdata.GetIntLeft();
int inte_right=pmtdata.GetIntRight();
float crosstime=0;
for(unsigned int i=inte_left;i<inte_right;i++){
//for(unsigned int i=500;i<800;i++){//calculate the cross time of primary pulse in fix window.
if(baseline-3<waveframe[i]*range_factor)continue;
else{
	if(baseline-3>waveframe[i-1]*range_factor)continue;
	for(int j=1;j<6;j++){
		if(baseline-3<waveframe[i+j]*range_factor)break;
	}
	//crosstime=i+(waveframe[i]*range_factor-baseline+3)/(waveframe[i]-waveframe[i+1])/range_factor;
	crosstime=i;
  pmtdata.Fillptc(crosstime);
	i=i+20;break;
	//crosstime=float(i);
}
}

//filling the 2D plot
//if(crosstime<inte_right-50&&crosstime>inte_left)pmtdata.Fillwf2d(waveframe);
pmtdata.Fillwf2d(waveframe);
//if(pmtdata.Getsignal_crosstime()>570)pmtdata.Fillwf2d(waveframe);






//if(crosstime>inte_right-10&&crosstime<inte_right+60)pmtdata.Fillwf2d(waveframe);
//if(crosstime<inte_left||crosstime>inte_right-10)pmtdata.Fillwf2d(waveframe);
//crosstime=0;
//for(int i=inte_left;i<inte_right;i++){
//if(baseline-3<waveframe[i]*range_factor)continue;
//else{
//	if(baseline-3>waveframe[i-1]*range_factor)continue;
//	for(int j=1;j<6;j++){
//		if(baseline-3<waveframe[i+j]*range_factor)break;
//	}
//	crosstime=i+(waveframe[i]*range_factor-baseline+3)/(waveframe[i]-waveframe[i+1])/range_factor;
// // crosstime=min_element(waveframe.begin()+500,waveframe.begin()+800)-waveframe.begin();
// //crosstime=min_element(waveframe.begin()+inte_left,waveframe.begin()+inte_right)-waveframe.begin();
//	//crosstime=float(i);
//	i=i+20;
//}
//}
//cout<<"lect:"<<inte_left<<" "<<inte_right<<" "<<crosstime<<endl;
pmtdata.Setsignal_crosstime(crosstime);
//signal_flag
//crosstime
//risetime
//falltime
//FWHM
//peak
//dqdc    //the dynamic intergratrion


//for(int i=0;i<10008;i++){
//if(baseline-3<waveframe[i]*0.12)continue;
//else{

//	for(int j=1;j<100;j++){
//		if(baseline-3<waveframe[i+j]*0.12)break;
//qdc+=baseline-waveframe[i+j]*0.12
//	}

//if(*min_element(waveframe.begin(), waveframe.end())<1300){
//cout<<baseline<<"inte:"<<inte_left<<" "<<inte_right<<endl;

  //for(int i=0;i<200;i++){
  //baseline+=waveframe[inte_left-i]*range_factor;
  //}
  //baseline=baseline/200.;
  //cout<<baseline<<"inte:"<<inte_left<<" "<<inte_right<<endl;
  int minPosition = min_element(waveframe.begin()+500,waveframe.begin()+800)-waveframe.begin();
//cout<<baseline<<"inte:"<<inte_left<<" "<<inte_right<<endl;
//cout<<"peak"<<minPosition<<endl;
//float theqdc=0;

//for(int i=0;1<85;i++){
//theqdc+=1389-waveframe[i+600-55]*range_factor;
//cout<<"qdc:"<<theqdc<<endl;}
  pmtdata.Setsignal_QDC((baseline*120-0.12*accumulate(std::begin(waveframe)+minPosition-50,std::begin(waveframe)+minPosition+70,0.0))/50./1.6);

  //cout<<"peak:"<<minPosition<<endl;
//}
//fixqdc=    //the fixed window integration


//float theqdc=0;
//cout<<baseline<<"inte:"<<inte_left<<" "<<inte_right<<endl;
//baseline=0;
//for(int i=0;i<200;i++){
//baseline+=waveframe[inte_left-i]*range_factor;
//}
//baseline=baseline/200.;
//for(int i=inte_left;i<inte_right;i++){
	//theqdc+=baseline-waveframe[i]*range_factor;
 //cout<<"qdc:"<<theqdc<<endl;
//}


//pmtdata.Setsignal_QDC(theqdc/50./1.6);
//pmtdata.Setsignal_QDC((baseline*(inte_right-inte_left)-range_factor*accumulate(std::begin(waveframe)+inte_left,std::begin(waveframe)+inte_right,0.0))/50.);
//if(inte_left>200 )pmtdata.Setsignal_QDC((baseline*(inte_right-inte_left)-range_factor*accumulate(waveframe.begin()+inte_left,waveframe.begin()+inte_right,0.0))/50.);
//cnn_flag
//set .....

//time and charge of afterpulses.
//8 pulses max
float aptime[20]={0};
float apcharge[20]={0};
//for(int pp=0;pp<8;pp++){
//aptime[pp]=pp*10;
//apcharge[pp]=pp;
//}
float xbar=(crosstime+500+waveframe.size())/2.;
float ybar=accumulate(std::begin(waveframe)+crosstime+500,std::end(waveframe),0.0)/(waveframe.size()-crosstime-500);
float apbn=0;
float apbd=0;
for(int ai=crosstime+500;ai<waveframe.size();ai++){
apbn+=(ai*waveframe[ai]-xbar*ybar);
apbd+=ai*ai-xbar*xbar;
}
float apb=apbn/apbd;
float apa=ybar-apb*xbar;
float threshold[10008]={0};
int apid=0;
float thischarge=0;
for(int ai=crosstime+500;ai<waveframe.size()-20;ai++){
threshold[ai]=(apa+apb*ai)-3./range_factor;
if(waveframe[ai]*range_factor>threshold[ai]*range_factor)continue;
else{
	if(threshold[ai]*range_factor>waveframe[ai-1]*range_factor)continue;
	for(int j=1;j<6;j++){
		if(threshold[ai]*range_factor<waveframe[ai+j]*range_factor)break;
	}
//	aptime[apid]=ai;
  
  for(int bi=0;bi<75;bi++){
  thischarge+=(((apa+apb*bi-waveframe[ai-25+bi])*range_factor)/50./1.6);  
  }
  //cout<<"apa,apb,apid,apcharge: "<<apa<<" "<<apb<<" "<<apid<<" "<<thischarge<<endl;
// apcharge[apid]=thischarge;
 
  thischarge=0; 
	ai=ai+20;
  apid++;
  //cout<<"thischarge:"<<thischarge<<endl;
 // break;
	//crosstime=float(i);
}
if(apid>19)break;
}
int npeaks=40;
// use tspectrum to search peaks
 TH1F *hspectrum = new TH1F("hspectrum","test",waveframe.size()-crosstime,crosstime-200,waveframe.size()-200);
// TH1F *hspectrum = new TH1F("hspectrum","test",waveframe.size()-520-crosstime,crosstime+500,waveframe.size()-200);
 for(int mm=0;mm<waveframe.size()-crosstime-1;mm++){
	 //double tmpv=0;
	 //for(int ai=0;ai<10;ai++){
	 //tmpv+=baseline-waveframe[crosstime-200+mm+ai-5];
	 //}
	 //hspectrum->SetBinContent(mm,tmpv);
	 hspectrum->SetBinContent(mm,(waveframe[crosstime-200+mm]));
//	 hspectrum->SetBinContent(mm,(2*baseline-waveframe[crosstime-200+mm]));
 }
 TSpectrum *s = new TSpectrum(5*npeaks);
 Int_t nfound = s->Search(hspectrum,4,"",0.0015);
// Int_t nfound = s->Search(hspectrum,0.5,"",0.001);  //this one works
// Int_t nfound = s->Search(hspectrum,5,"noMarkov",0.05);
// cout<<" the n found"<<nfound<<endl;
TH1 *hb = s->Background(hspectrum,20,"same");
//if (hb) c1->Update();
//if (np <0) return;

Double_t *xpeaks;
xpeaks = s->GetPositionX();
for (int p=0;p<nfound;p++) {
Double_t xp = xpeaks[p];
//cout<<"xps"<<xp<<endl;
Int_t bin =hspectrum->GetXaxis()->FindBin(xp);
if(p<20){aptime[p]=bin+crosstime-200-crosstime;  //the time delay with primary pulse
	//if(bin+crosstime+500<9000)cout<<"the bin"<<bin<<endl;
	    // if(bin<1500||bin>8500)continue;
		apcharge[p]=0.12*(hb->GetBinContent(bin+crosstime+500)*120-accumulate(std::begin(waveframe)+bin+crosstime+500-50,std::begin(waveframe)+bin+crosstime+500+70,0.0))/50./1.6;
//		if(bin<8000)cout<<"the bin"<<bin<<"  "<<bin+crosstime+500-crosstime<<endl;
		//cout<<"the bin cont"<<hb->GetBinContent(bin+crosstime+500)<<endl;
		//cout<<"the ap charge"<<apcharge[p]<<endl;
//			(baseline*120-0.12*accumulate(std::begin(waveframe)+minPosition-50,std::begin(waveframe)+minPosition+70,0.0))/50./1.6
	}
} 



delete s;
delete hspectrum;
pmtdata.Setaptime(aptime);
pmtdata.Setapcharge(apcharge);


	return 0;
}






//Int_t ProcessWaveFrame( vector<int> waveframe,APdata &apd,float range_factor) {
////primaryPulsePeakPosition=231;
////primaryPulseTree->Fill();  
//int lengthOfEachWave = __WaveLength; 
//float global_bsl=0;
//for(int t=8000;t<lengthOfEachWave;t++){
//global_bsl+=waveframe[t];
//}
//global_bsl=global_bsl/(lengthOfEachWave-8000);
//if(global_bsl>1410./0.12)
//cout<<"gbsl"<<global_bsl<<"rf"<<range_factor<<endl;
//vector<int> ppw=vector<int>(waveframe.begin(),waveframe.begin()+__PrimaryPeakRightMost);
////cout<<ppw[12]<<endl;
//apd.Fillpp2d(ppw);
//apd.SetWave_valid(1);
////__PrimaryPeakLeftMost
//vector<int>::iterator maxPosition = min_element(waveframe.begin(), waveframe.begin()+__PrimaryPeakRightMost);
////cout<<"mmmmmmm"<<maxPosition-waveframe.begin()<<endl;
//if(maxPosition-waveframe.begin()<=500)apd.SetWave_valid(0);
//int falledge[__EdgefitPoints]={0};
//int tfalledge[__EdgefitPoints]={0};
//int riseedge[__EdgefitPoints]={0};
//int triseedge[__EdgefitPoints]={0};
//float a0,a1,b0,b1;
////cout<<"dfd"<<maxPosition- waveframe.begin()<<endl;
//for(int ii=0;ii<__EdgefitPoints;ii++){
//falledge[ii]=*(maxPosition+ii);
//riseedge[ii]=*(maxPosition-ii);
//tfalledge[ii]=maxPosition-waveframe.begin()+ii;
//triseedge[ii]=maxPosition-waveframe.begin()-ii;
////cout<<tfalledge[ii]<<" "<<falledge[ii]<<endl;
//}
//Min2Method(a0,a1,tfalledge,falledge,__EdgefitPoints);
//Min2Method(b0,b1,triseedge,riseedge,__EdgefitPoints);
//int pp_inte_end=(int)(global_bsl-a0)/a1;
//if(pp_inte_end>__PrimaryPeakRightMost||pp_inte_end<__PrimaryPeakLeftMost)
//{pp_inte_end=__PrimaryPeakRightMost;apd.SetWave_valid(0);}
//int pp_inte_start=(int)(global_bsl-b0)/b1;
//if(pp_inte_start>__PrimaryPeakRightMost||pp_inte_start<__PrimaryPeakLeftMost)
//{pp_inte_start=__PrimaryPeakLeftMost;apd.SetWave_valid(0);}
//if(pp_inte_end<=pp_inte_start)
//{pp_inte_end=__PrimaryPeakRightMost;
//pp_inte_start=__PrimaryPeakLeftMost;apd.SetWave_valid(0);}
////calculate charge of pp
//int pp_charge=global_bsl*(pp_inte_end-pp_inte_start)-accumulate(waveframe.begin()+pp_inte_start,waveframe.begin()+pp_inte_end,0);
//float pp_peak=global_bsl-*maxPosition;
//if(*maxPosition<=1)apd.SetWave_valid(0);
//int peakpos=maxPosition - waveframe.begin();
//int fwhm_rise=pp_inte_start;
//int fwhm_fall=pp_inte_end;
//int pp_tc;
//float pp_bsl;
//for(int pi=0;pi<(pp_inte_end-peakpos);pi++){
//if(waveframe[peakpos+pi]<global_bsl-.5*pp_peak&&waveframe[peakpos+pi+1]>=global_bsl-.5*pp_peak )
//fwhm_fall=peakpos+pi;
//if(waveframe[peakpos-pi]<global_bsl-.5*pp_peak&&waveframe[peakpos-pi-1]>=global_bsl-.5*pp_peak )
//fwhm_rise=peakpos-pi;
//}
//if(fwhm_fall-fwhm_rise>40)apd.SetWave_valid(0);
////apd.SetWave_valid(0);
////cout<<"pppppp"<<peakpos<<endl;
//for(int ci=0;ci<2*(peakpos-pp_inte_start);ci++){
//if(waveframe[peakpos-ci]<global_bsl-__AfterpulseThreshold&&waveframe[peakpos-ci-1]>=global_bsl-__AfterpulseThreshold)
//{pp_tc=peakpos-ci;break;}
//}
//if(pp_tc<__PrimaryPeakLeftMost||pp_tc>__PrimaryPeakRightMost)apd.SetWave_valid(0);
//if(__PrimaryBaselineLeft+pp_inte_start>0)
//{
//pp_bsl=1.*accumulate(waveframe.begin()+pp_inte_start+__PrimaryBaselineLeft,waveframe.begin()+pp_inte_start+__PrimaryBaselineRight,0)/(__PrimaryBaselineRight-__PrimaryBaselineLeft);
//}
//else{pp_bsl=global_bsl;}
//if(pp_peak<50||fwhm_fall-fwhm_rise>70)apd.SetWave_valid(0);
////cout<<"asssa"<<a0<<" "<<a1<<" "<<tfalledge[0]<<" "<<pp_inte_start<<" "<<pp_inte_end<<endl;
////begin calculate the ap
//int ap_count=0;
//int ap_pp=0;
//int ap_fwhm=0;
//int fwhma1=0;
//int fwhma2=0;
//float ap_bsl=0;
//int ap_tc;
//float ap_peak;
//float ap_charge;
//float ap_bslcheck=0;
//
//for(int i=pp_inte_end+__AfterpulseSearchLeft;i<__AfterpulseSearchRight;i++){
//int isriseedge=1;
////cout<<"dffd"<<ap_bsl<<"  "<<ap_bslcheck<<endl;
////ap_bsl=accumulate(waveframe.begin()+i-30,waveframe.begin()+i-10,0)/20.;  
////ap_bsl=global_bsl;
//for(int j=1;j<8;j++){
////if(waveframe[i+j]>ap_bsl-__AfterpulseThreshold){isriseedge=0;break;}
//if(waveframe[i+j]>global_bsl-__AfterpulseThreshold){isriseedge=0;break;}
//}
////cout<<"gbbl"<<global_bsl-__AfterpulseThreshold<<endl;
////cout<<"found it"<<isriseedge<<endl;
////if(waveframe[i]>ap_bsl-__AfterpulseThreshold&&isriseedge){
//if(waveframe[i]>global_bsl-__AfterpulseThreshold&&isriseedge){
//if(ap_count>=5)break;
//vector<int>::iterator apmaxPosition = min_element(waveframe.begin()+i, waveframe.begin()+i+30);
//ap_pp=apmaxPosition-waveframe.begin();
//ap_tc=i;
////cout<<"ccccc1"<< ap_tc<<endl;
//ap_bsl=accumulate(waveframe.begin(),waveframe.begin()+300,0)/300.;  
//ap_bslcheck=accumulate(waveframe.begin()+1500,waveframe.begin()+2500,0)/1000.;  
//ap_peak=ap_bsl-*apmaxPosition;
//
//for(int ff=0;ff<__AfterpulsePeakIntRight-__AfterpulsePeakIntLeft;ff++){
//if(waveframe[ap_tc+ff]>ap_bsl-.5*ap_peak&&waveframe[ap_tc+ff+1]<=ap_bsl-.5*ap_peak)fwhma1=ff;
//if(ap_tc+ff>ap_pp&&waveframe[ap_tc+ff]<ap_bsl-.5*ap_peak&&waveframe[ap_tc+ff+1]>=ap_bsl-.5*ap_peak)fwhma2=ff;
//}
//ap_fwhm=fwhma2-fwhma1;
////if(ap_fwhm<3)break;
////if(ap_peak<3||ap_peak>pp_peak)break;
////if(fabs(ap_bsl-global_bsl)>20)break;
//ap_charge=ap_bsl*(__AfterpulsePeakIntRight-__AfterpulsePeakIntLeft)-accumulate(apmaxPosition+__AfterpulsePeakIntLeft,apmaxPosition+__AfterpulsePeakIntRight,0);
////if(ap_charge<0)break;
////cout<<"sdsffs"<<ap_pp<<" "<<ap_bsl<<endl;
////if(ap_tc>800&&ap_tc<2500)apd.Fillwf2d(waveframe);
////if(range_factor)
//if(range_factor&&(ap_bslcheck-ap_bsl<19.2))
//apd.Fillwf2d(waveframe);
//if(ap_tc>3000&&ap_tc<3300&&apd.Getwave1d_flag()==0&&apd.GetWave_valid()&&pp_bsl-global_bsl!=0){
//apd.Fillwf1d(waveframe);
//cout<<pp_tc<<" "<<global_bsl<<" "<<pp_bsl<<" "<<pp_peak<<endl;
//}
//apd.Setap( ap_pp,ap_fwhm,ap_bsl,float(ap_tc-pp_tc),ap_peak,ap_charge,ap_count );
//if(ap_peak>30&&pp_peak>50)apd.Fillapt(float(ap_tc-pp_tc),ap_charge);
////cout<<"sfdgfdg"<<float(ap_tc)-float(pp_tc)<<endl;
//i+=30;  ///junp to next
//ap_count++;
//}
////if(range_factor&&(ap_bslcheck-ap_bsl<2))
//for(int ac=ap_count;ac<5;ac++){
//apd.Setap( 0,0,0,0,0,0,ac );
//}
//
//}
//apd.Setpp(-1,ap_count,global_bsl,peakpos,fwhm_fall-fwhm_rise,pp_bsl,pp_tc,pp_peak,pp_charge);
////for(int api=0;api<5;api++){
////apd.Setap( waveframe[3]+api,waveframe[3]+api*2,waveframe[4],waveframe[5],waveframe[6],waveframe[7],api );
////apd.Setap(int ap_pp,int ap_fwhm,float ap_bl,float ap_tc,float ap_peak,float ap_charge,int ap_index );
//////void Setpp(bool pp_wv,int pp_at,float pp_gb, int pp_pp,int pp_fwhm,float pp_bl,float pp_tc,float pp_peak,float pp_charge );
//
////}
//
//
////Fill the tree
//return 0;
//
//}
